﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Framework.Library.Components.Security;
using Framework.Library.Utilities;
using Framework.Module.Adsvertising.Lib.Data;
using Framework.Module.Adsvertising.Lib.DataAccess;

namespace Framework.Module.Adsvertising.Admin
{
    public partial class PositionList : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) 
                LoadPositions();
            lblUpdateStatus.Text = string.Empty;	
        }

        private void LoadPositions()
        {
            dgrPositions.Columns[1].HeaderText = (String)this.GetLocalResourceObject("Position");
            dgrPositions.Columns[2].HeaderText = (String)this.GetLocalResourceObject("Name");
            dgrPositions.Columns[3].HeaderText = (String)this.GetLocalResourceObject("Type");

            dgrPositions.DataSource = PositionDB.GetAll();
            dgrPositions.DataBind();
        }

        protected void dgrPositions_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "edit")
            {
                PositionInfo info = PositionDB.GetInfo(ConvertUtility.ToInt32(e.Item.Cells[0].Text));
                if (info == null) return;
                txtID.Text = info.Pos_ID.ToString();
                txtName.Text = info.Pos_Name;
                txtPosition.Text = info.Pos_Position;
                dropType.SelectedIndex = -1;
                MiscUtility.SetSelected(dropType.Items, info.Pos_Type);
                txtSeparateCode.Text = info.Pos_SeparateCode;
                txtWidth.Text = info.Pos_Width.ToString();
                txtHeight.Text = info.Pos_Height.ToString();
            }
            if (e.CommandName == "del")
            {
                try
                {
                    PositionDB.Delete(ConvertUtility.ToInt32(e.Item.Cells[0].Text));
                    lblUpdateStatus.Text = (String)this.GetLocalResourceObject("UpdateSuccess");
                    LoadPositions();
                }
                catch
                {
                    lblUpdateStatus.Text = (String)this.GetLocalResourceObject("UpdateError");
                }
            }
        }

        protected void dgrPositions_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0) return;
            DataRowView curData = (DataRowView)e.Item.DataItem;
            Button cmdDel = (Button)e.Item.FindControl("cmdDel");
            cmdDel.OnClientClick = "javascript:return confirm('" + (String)this.GetLocalResourceObject("DeleteConfirm")+ "' )";
            switch (e.Item.Cells[3].Text)
            {
                case "rotator":
                    e.Item.Cells[3].Text = (String)this.GetLocalResourceObject("rotator"); ;
                    break;
                case "random":
                    e.Item.Cells[3].Text = (String)this.GetLocalResourceObject("random"); ;
                    break;
                case "popup":
                    e.Item.Cells[3].Text = (String)this.GetLocalResourceObject("popup"); ;
                    break;
                default:
                    e.Item.Cells[3].Text = (String)this.GetLocalResourceObject("default"); ;
                    break;
            }
        }
       
        protected void btnEmpty_Click(object sender, EventArgs e)
        {
            txtID.Text = string.Empty;
            txtName.Text = string.Empty;
            txtPosition.Text = string.Empty;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            PositionInfo info = PositionDB.GetInfo(ConvertUtility.ToInt32(txtID.Text));
            info.Pos_Position = txtPosition.Text.Trim();
            info.Pos_Type = dropType.SelectedValue;
            info.Pos_Name = txtName.Text.Trim();
            info.Pos_SeparateCode = txtSeparateCode.Text.Trim();
            info.Pos_Width = ConvertUtility.ToInt32(txtWidth.Text);
            info.Pos_Height = ConvertUtility.ToInt32(txtHeight.Text);
            try
            {
                PositionDB.Update(info);
                lblUpdateStatus.Text = (String)this.GetLocalResourceObject("UpdateSuccess");
                LoadPositions();
            }
            catch
            {
                lblUpdateStatus.Text = (String)this.GetLocalResourceObject("UpdateError");
            }
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {

            PositionInfo info = new PositionInfo();
            info.Pos_Type = dropType.SelectedValue;
            info.Pos_Name = txtName.Text.Trim();            
            info.Pos_SeparateCode = txtSeparateCode.Text.Trim();
            info.Pos_Width = ConvertUtility.ToInt32(txtWidth.Text);
            info.Pos_Height = ConvertUtility.ToInt32(txtHeight.Text);
            info.Pos_Position = txtPosition.Text.Trim();
            try
            {

                PositionDB.Insert(info);
                lblUpdateStatus.Text = (String)this.GetLocalResourceObject("UpdateSuccess");
                LoadPositions();
            }
            catch
            {
                lblUpdateStatus.Text = (String)this.GetLocalResourceObject("UpdateError");
            }
        }

    }
}